package cn.redcoral.messageplus.data.service.impl;

import cn.redcoral.messageplus.data.model.message.Message;
import cn.redcoral.messageplus.data.model.entity.HistoryMessage;
import cn.redcoral.messageplus.data.mapper.MessagePlusHistoryMessageMapper;
import cn.redcoral.messageplus.data.service.HistoryMessageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 历史消息 Service
 * @author mo
 **/
@Service
public class HistoryMessageServiceImpl implements HistoryMessageService {

    @Autowired
    private MessagePlusHistoryMessageMapper messageMapper;

    @Transactional
    @Override
    public String insertMessage(cn.messageplus.core.message.Message message) {
        HistoryMessage historyMessage = new HistoryMessage(message);
        messageMapper.insert(historyMessage);
        return historyMessage.getId();
    }


    @Transactional
    @Override
    public Long insertMessage(Message message,boolean isFail) {
        // 消息创建时间不能为空
        if (message.getCreateTime() == null) return null;
        // 插入并返回结果
//        HistoryMessage historyMessage = new HistoryMessage(message, isFail);
//        messageMapper.insert(historyMessage);
//        return historyMessage.getId();
        return null;
    }

    @Transactional
    @Override
    public boolean updateMessage(Long id, boolean isFail) {
        UpdateWrapper<HistoryMessage> lpw = new UpdateWrapper<>();
        lpw.eq("id",id);
        lpw.set("is_fail",isFail);
        int isOk = messageMapper.update(lpw);
        return isOk>0;
    }

    @Override
    public void removeMessageBySenderId(String senderId) {
        LambdaQueryWrapper<HistoryMessage> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(HistoryMessage::getSenderId, senderId);
        messageMapper.delete(lqw);
    }

    @Override
    public List<Message> popMessageBySenderId(String senderId) {
        LambdaQueryWrapper<HistoryMessage> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(HistoryMessage::getSenderId, senderId);
        List<HistoryMessage> historyMessageList = messageMapper.selectList(lqw);
        messageMapper.delete(lqw);
        return Message.BuildHistoryMessageList(historyMessageList);
    }

    @Override
    public List<Message> popMessageByReceiverId(String receiverId) {
        LambdaQueryWrapper<HistoryMessage> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(HistoryMessage::getReceiverId, receiverId);
        List<HistoryMessage> historyMessageList = messageMapper.selectList(lqw);
        messageMapper.delete(lqw);
        return Message.BuildHistoryMessageList(historyMessageList);
    }

    @Override
    public List<Message> selectMessageBySenderId(String senderId) {
        LambdaQueryWrapper<HistoryMessage> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(HistoryMessage::getSenderId, senderId);
        List<HistoryMessage> historyMessageList = messageMapper.selectList(lqw);
        return Message.BuildHistoryMessageList(historyMessageList);
    }

    @Override
    public List<Message> selectMessageByReceiverId(String receiverId) {
        LambdaQueryWrapper<HistoryMessage> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(HistoryMessage::getReceiverId, receiverId);
        List<HistoryMessage> historyMessageList = messageMapper.selectList(lqw);
        return Message.BuildHistoryMessageList(historyMessageList);
    }
}
